package com.guodu.dao.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialJoinStoreDao;
import com.guodu.entity.MaterialJoinStore;
import com.guodu.entity.MaterialjoinstoreStatus;
import com.guodu.entity.Store;
import com.guodu.entity.Units;
import com.guodu.vo.MaterialJoinStoreQueryVo;

@Repository("materialJoinStroeDao")
public class MaterialJoinStoreDaoImpl extends BaseDao implements MaterialJoinStoreDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialJoinStore> findByVo(MaterialJoinStoreQueryVo materialJoinStoreQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialJoinStore m where 1=1");
		if(materialJoinStoreQueryVo!=null){
			if(materialJoinStoreQueryVo.getDrawingnum()!=null&&materialJoinStoreQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialJoinStoreQueryVo.getDrawingnum()+"%'");
			}
			if(materialJoinStoreQueryVo.getFunitsName()!=null&&materialJoinStoreQueryVo.getFunitsName().trim().length()>0){
				hql.append(" and m.funitsName like '%"+materialJoinStoreQueryVo.getFunitsName()+"%'");
			}
			if(materialJoinStoreQueryVo.getGroupName()!=null&&materialJoinStoreQueryVo.getGroupName().trim().length()>0){
				hql.append(" and m.groupName like '%"+materialJoinStoreQueryVo.getGroupName()+"%'");
			}
			if(materialJoinStoreQueryVo.getIndustry()!=null&&materialJoinStoreQueryVo.getIndustry().trim().length()>0){
				hql.append(" and m.industry like '%"+materialJoinStoreQueryVo.getIndustry()+"%'");
			}
			if(materialJoinStoreQueryVo.getJointShareUnits()!=null&&materialJoinStoreQueryVo.getJointShareUnits().trim().length()>0){
				hql.append(" and m.jointshareUnits like '%"+materialJoinStoreQueryVo.getJointShareUnits()+"%'");
			}
			if(materialJoinStoreQueryVo.getTheSystem()!=null&&materialJoinStoreQueryVo.getTheSystem().trim().length()>0){
				hql.append(" and m.theSystem like '%"+materialJoinStoreQueryVo.getTheSystem()+"%'");
			}
			if(materialJoinStoreQueryVo.getUnitsName()!=null&&materialJoinStoreQueryVo.getUnitsName().trim().length()>0){
				hql.append(" and m.unitsName like '%"+materialJoinStoreQueryVo.getUnitsName()+"%'");
			}
			if(materialJoinStoreQueryVo.getMaterialCode()!=null&&materialJoinStoreQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and m.materialCode like '%"+materialJoinStoreQueryVo.getMaterialCode()+"%'");
			}
			if(materialJoinStoreQueryVo.getMaterialName()!=null&&materialJoinStoreQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and m.materialName like '%"+materialJoinStoreQueryVo.getMaterialName()+"%'");
			}
			if(materialJoinStoreQueryVo.getSpecifications()!=null&&materialJoinStoreQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and m.specifications like '%"+materialJoinStoreQueryVo.getSpecifications()+"%'");
			}
			if(materialJoinStoreQueryVo.getSupplier()!=null&&materialJoinStoreQueryVo.getSupplier().trim().length()>0){
				hql.append(" and m.supplier like '%"+materialJoinStoreQueryVo.getSupplier()+"%'");
			}
			if(materialJoinStoreQueryVo.getTexture()!=null&&materialJoinStoreQueryVo.getTexture().trim().length()>0){
				hql.append(" and m.texture like '%"+materialJoinStoreQueryVo.getTexture()+"%'");
			}
			if(materialJoinStoreQueryVo.getCreateBeginTime()!=null&&materialJoinStoreQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and m.createTime >= TO_DATE('"+materialJoinStoreQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialJoinStoreQueryVo.getCreateEndTime()!=null&&materialJoinStoreQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and m.createTime <= TO_DATE('"+materialJoinStoreQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialJoinStoreQueryVo.getStatus()!=null&&materialJoinStoreQueryVo.getStatus().trim().length()>0&&!materialJoinStoreQueryVo.getStatus().equals("5")){
				if(materialJoinStoreQueryVo.getStatus().equals("10")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))");
				}else if(materialJoinStoreQueryVo.getStatus().equals("20")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=2))");
				}else{
					hql.append(" and m.status ="+materialJoinStoreQueryVo.getStatus());
				}
			}
		}
		if (units == null) {
			hql.append(" and ((m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))) or status=1 or status=3 or status=4)");
		} 
		hql.append(" and store = '" + materialJoinStoreQueryVo.getStoreId() + "'");
		hql.append(" order by storeId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public int findSizeByVo(
			MaterialJoinStoreQueryVo materialJoinStoreQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from MaterialJoinStore m where 1=1");
		if(materialJoinStoreQueryVo!=null){
			if(materialJoinStoreQueryVo.getDrawingnum()!=null&&materialJoinStoreQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialJoinStoreQueryVo.getDrawingnum()+"%'");
			}
			if(materialJoinStoreQueryVo.getFunitsName()!=null&&materialJoinStoreQueryVo.getFunitsName().trim().length()>0){
				hql.append(" and m.funitsName like '%"+materialJoinStoreQueryVo.getFunitsName()+"%'");
			}
			if(materialJoinStoreQueryVo.getGroupName()!=null&&materialJoinStoreQueryVo.getGroupName().trim().length()>0){
				hql.append(" and m.groupName like '%"+materialJoinStoreQueryVo.getGroupName()+"%'");
			}
			if(materialJoinStoreQueryVo.getIndustry()!=null&&materialJoinStoreQueryVo.getIndustry().trim().length()>0){
				hql.append(" and m.industry like '%"+materialJoinStoreQueryVo.getIndustry()+"%'");
			}
			if(materialJoinStoreQueryVo.getJointShareUnits()!=null&&materialJoinStoreQueryVo.getJointShareUnits().trim().length()>0){
				hql.append(" and m.jointshareUnits like '%"+materialJoinStoreQueryVo.getJointShareUnits()+"%'");
			}
			if(materialJoinStoreQueryVo.getTheSystem()!=null&&materialJoinStoreQueryVo.getTheSystem().trim().length()>0){
				hql.append(" and m.theSystem like '%"+materialJoinStoreQueryVo.getTheSystem()+"%'");
			}
			if(materialJoinStoreQueryVo.getUnitsName()!=null&&materialJoinStoreQueryVo.getUnitsName().trim().length()>0){
				hql.append(" and m.unitsName like '%"+materialJoinStoreQueryVo.getUnitsName()+"%'");
			}
			if(materialJoinStoreQueryVo.getMaterialCode()!=null&&materialJoinStoreQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and m.materialCode like '%"+materialJoinStoreQueryVo.getMaterialCode()+"%'");
			}
			if(materialJoinStoreQueryVo.getMaterialName()!=null&&materialJoinStoreQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and m.materialName like '%"+materialJoinStoreQueryVo.getMaterialName()+"%'");
			}
			if(materialJoinStoreQueryVo.getSpecifications()!=null&&materialJoinStoreQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and m.specifications like '%"+materialJoinStoreQueryVo.getSpecifications()+"%'");
			}
			if(materialJoinStoreQueryVo.getSupplier()!=null&&materialJoinStoreQueryVo.getSupplier().trim().length()>0){
				hql.append(" and m.supplier like '%"+materialJoinStoreQueryVo.getSupplier()+"%'");
			}
			if(materialJoinStoreQueryVo.getTexture()!=null&&materialJoinStoreQueryVo.getTexture().trim().length()>0){
				hql.append(" and m.texture like '%"+materialJoinStoreQueryVo.getTexture()+"%'");
			}
			if(materialJoinStoreQueryVo.getCreateBeginTime()!=null&&materialJoinStoreQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and m.createTime >= TO_DATE('"+materialJoinStoreQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialJoinStoreQueryVo.getCreateEndTime()!=null&&materialJoinStoreQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and m.createTime <= TO_DATE('"+materialJoinStoreQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialJoinStoreQueryVo.getStatus()!=null&&materialJoinStoreQueryVo.getStatus().trim().length()>0&&!materialJoinStoreQueryVo.getStatus().equals("5")){
				if(materialJoinStoreQueryVo.getStatus().equals("10")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))");
				}else if(materialJoinStoreQueryVo.getStatus().equals("20")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=2))");
				}else{
					hql.append(" and m.status ="+materialJoinStoreQueryVo.getStatus());
				}
			}
		}
		if (units == null) {
			hql.append(" and ((m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))) or status=1 or status=3 or status=4)");
		} 
		hql.append(" and store = '" + materialJoinStoreQueryVo.getStoreId() + "'");
		return super.findInt(hql.toString());
	}

	@Override
	public MaterialJoinStore findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialJoinStore) super.findById(MaterialJoinStore.class, id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialJoinStore.class, id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generx`ated method stub
		super.deleteAllById(MaterialJoinStore.class, ids);
	}

	@Override
	public void save(MaterialJoinStore entity) {
		// TODO Auto-generated method stub
		entity.setStoreId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		super.save(entity);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialJoinStore set status =");
		hql.append(status);
		hql.append(" where storeId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void update(MaterialJoinStore entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@Override
	public void changeStatus(List<MaterialjoinstoreStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialjoinstoreStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialjoinstoreStatus.class, "materialId", id);
	}

	@Override
	public void delByStoreId(String storeId) {
		// TODO Auto-generated method stub
		MaterialJoinStore materialJoinStore = new MaterialJoinStore();
		materialJoinStore.setStoreId(storeId);
		super.delete(materialJoinStore);
	}

	@Override
	public Store findStoreById(String id) {
		// TODO Auto-generated method stub
		return (Store) super.findById(Store.class, id);
	}


}
